<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Get the last inserted row: GNOME Data Access 5 manual</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="index.html" title="GNOME Data Access 5 manual">
<link rel="up" href="howto.html" title="HOWTO for common tasks">
<link rel="prev" href="howto-exec-non-select.html" title="Execute an INSERT, UPDATE or DELETE command">
<link rel="next" href="ch12s08.html" title="Execute a DDL command">
<meta name="generator" content="GTK-Doc V1.32 (XML mode)">
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="5"><tr valign="middle">
<td width="100%" align="left" class="shortcuts"></td>
<td><a accesskey="h" href="index.html"><img src="home.png" width="16" height="16" border="0" alt="Home"></a></td>
<td><a accesskey="u" href="howto.html"><img src="up.png" width="16" height="16" border="0" alt="Up"></a></td>
<td><a accesskey="p" href="howto-exec-non-select.html"><img src="left.png" width="16" height="16" border="0" alt="Prev"></a></td>
<td><a accesskey="n" href="ch12s08.html"><img src="right.png" width="16" height="16" border="0" alt="Next"></a></td>
</tr></table>
<div class="sect1">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="id-1.3.3.9"></a>Get the last inserted row</h2></div></div></div>
<p>
      <span class="application">Libgda</span> allows one to get the last inserted row right after an INSERT statement has been
      executed. <span class="application">Libgda</span> returns a new <a class="link" href="GdaSet.html" title="GdaSet">GdaSet</a> object (which the caller
      must unref when not needed anymore) which contains named values, one for each column of the
      table in which data has been inserted. To get that object, pass a place holder to the
      <em class="parameter"><code>last_insert_row</code></em> of
      <a class="link" href="GdaConnection.html#gda-connection-statement-execute-non-select" title="gda_connection_statement_execute_non_select ()">gda_connection_statement_execute_non_select()</a>
    </p>
<p>
      The following code example show how to use the returned <a class="link" href="GdaSet.html" title="GdaSet">GdaSet</a> object:
      </p>
<pre class="programlisting">
GdaSet *last_row;
GdaStatement *stmt;
stmt = gda_sql_parser_parse_string (parser, "INSERT INTO mytable (name) VALUES ('joe')", NULL, NULL);
if (gda_connection_statement_execute_non_select (connection, stmt, NULL, &amp;last_row, &amp;error) == -1) {
    g_warning ("Can't execute INSERT: %s\n",
              error &amp;&amp; error-&gt;message ? error-&gt;message : "???");
    if (error)
        g_error_free (error);
}
else {
    if (!last_row)
        g_print ("Last row not reported\n");
    else {
        GSList *list;
        for (list = last_row-&gt;holders; list; list = list-&gt;next) {
            GdaHolder *h = GDA_HOLDER (list-&gt;data);
            gchar *str;
            str = gda_value_stringify (gda_holder_get_value (h));
            g_print ("\t%s =&gt; %s\n", gda_holder_get_id (h), str);
            g_free (str);
        }
    g_object_unref (last_row);
}
g_object_unref (stmt);
      </pre>
<p>
      Which gives the following output (considering that in the example the "mytable" table has two columns: an Id
      and a Name columns):
      </p>
<pre class="programlisting">
        +0 =&gt; 1
        +1 =&gt; joe
      </pre>
<p>
    </p>
</div>
<div class="footer">
<hr>Generated by GTK-Doc V1.32</div>
</body>
</html>